package cn.tangsihai.user.controller;

import cn.tangsihai.basic.exception.MyException;
import cn.tangsihai.basic.util.AjaxResult;
import cn.tangsihai.user.dto.LoginDto;
import cn.tangsihai.user.service.ILoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 后台登录
 */
@RestController
@RequestMapping("/login")
@Api(value = "后台登录接口")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @PostMapping("/account")
    @ApiOperation(value = "后台登录")
    public AjaxResult account(@RequestBody LoginDto loginDto){
        try {
            Map<String, Object> map = loginService.account(loginDto);
            return AjaxResult.me().setResultObj(map); // 将登录信息响应回前端
        } catch (MyException e) {
            return AjaxResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常");
        }
    }

    /**
     * 微信登录
     * @return
     */
    @PostMapping("/wechat")
    @ApiOperation(value = "后台登录")
    public AjaxResult wechat(@RequestBody Map<String, String> params){
        try {
            return loginService.wechat(params);
        } catch (MyException e) {
            return AjaxResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常");
        }
    }

    /**
     * 第一次微信登录绑定业务
     * @param map
     * @return
     */
    @RequestMapping("/binder/wechat")
    public AjaxResult bander (@RequestBody Map<String, String> map){
        try {
            return loginService.bander(map);
        } catch (MyException e) {
            return AjaxResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统异常");
        }
    }
}
